package com.lin.filegraph.utils.extractor;

import com.lin.filegraph.utils.extractor.DependencyExtractor;
import com.lin.filegraph.utils.filedg.FileGraph;
import com.lin.filegraph.utils.model.ProjectInfo;
import com.lin.filegraph.utils.parser.astutil.JSourceParser;
import com.lin.filegraph.utils.parser.node.JProjectNode;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 10:58
 */

public class JavaInfoExtractor {


    public static FileGraph getFileGraph(String projectPath) {
        String projectName = "";
        // 创建AST
        String versionNo = "";
        System.out.println("开始生成AST");
        JProjectNode root = new JSourceParser().createSimpleAST(projectName, versionNo, projectPath);
        DependencyExtractor dependencyExtractor = new DependencyExtractor(projectPath, root);
        dependencyExtractor.calculateDependencyOfFiles();
        System.out.println("文件依赖图生成完毕");
        FileGraph fGraph = root.getFdg();

        //fGraph为文件依赖图
        return fGraph;
    }

    public static JProjectNode getJProjectNode(String projectPath) {
        String projectName = "";
        // 创建AST
        String versionNo = "";
        System.out.println("开始生成AST");
        JProjectNode root = new JSourceParser().createSimpleAST(projectName, versionNo, projectPath);
        System.out.println("AST生成完毕");
        return root;
    }

    public static FileGraph getFileGraph(String projectPath, JProjectNode root, int loc) {

        // 对依赖关系提取与整合，得到文件依赖图
        System.out.println("开始生成文件依赖图");
        DependencyExtractor dependencyExtractor = new DependencyExtractor(projectPath, root);
        dependencyExtractor.calculateDependencyOfFiles();
        System.out.println("文件依赖图生成完毕");
        FileGraph fGraph = root.getFdg();
        fGraph.filterloc(loc);
        root.setFdg(fGraph);
        //fGraph为文件依赖图
        return fGraph;

    }

    public static FileGraph getFileGraph(String projectPath, JProjectNode root, int loc, String depend, boolean testFilter) {

        // 对依赖关系提取与整合，得到文件依赖图
        System.out.println("开始生成文件依赖图");

        DependencyExtractor dependencyExtractor = new DependencyExtractor(projectPath, root);
        dependencyExtractor.calculateDependencyOfFiles();

        System.out.println("文件依赖图生成完毕");

        FileGraph fGraph = root.getFdg();
        fGraph.filterloc(loc);
        if (testFilter) {
            fGraph.filterTest();
        }
        fGraph.filterDepend(depend);
        root.setFdg(fGraph);
        //fGraph为文件依赖图
        return fGraph;

    }

//
//    public static FileGraphModel change(String json) {//将json转为前端需要的格式
//
//        JSONObject jsonObject = JSONObject.parseObject(json);
//        JSONArray Node_Array = jsonObject.getJSONArray("ListOfFile");
//        List<FdgNodeModel> ListOfFile = new ArrayList<>();
//        for (int i = 0; i < Node_Array.size(); i++) {
//            JSONObject object = Node_Array.getJSONObject(i);
//            String id = object.getString("id");
//            String name = object.getString("FileName");
//            List<String> category = new ArrayList<>();
//            String type = object.getString("FileType");
//            String absolutePath = object.getString("AbsolutePath");
//            FdgNodeModel fdgNodeModel = new FdgNodeModel(id, category, name, type, absolutePath);
//            ListOfFile.add(fdgNodeModel);
//        }
//
//        List<FdgEdgeModel> ListOfLink = new ArrayList<>();
//        JSONArray Edge_Array = jsonObject.getJSONArray("ListOfLink");
//        for (int i = 0; i < Edge_Array.size(); i++) {
//            JSONObject object = Edge_Array.getJSONObject(i);
//            String source = object.getString("source");
//            String target = object.getString("target");
//            FdgEdgeModel fdgEdgeModel = new FdgEdgeModel(source, target);
//            ListOfLink.add(fdgEdgeModel);
//        }
//        List<FdgNodeModel> ListOfNode = new ArrayList<>();
//        for (FdgNodeModel node : ListOfFile) {
//            List<String> categories = new ArrayList<>();
//            for (FdgEdgeModel edge : ListOfLink) {
//                if (edge.getTarget().equals(node.getId())) {
//                    categories.add(edge.getSource());
//                }
//            }
//            FdgNodeModel fdgNodeModel = new FdgNodeModel(node.getId(), categories, node.getName(), node.getType(), node.getAbsolutePath());
//            ListOfNode.add(fdgNodeModel);
//        }
//        FileGraphModel fileGraphModel = new FileGraphModel(ListOfNode, ListOfLink);
////        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(fileGraphModel);
////        String jsonStr = jsonArray.toString();
//        return fileGraphModel;
////        return jsonStr;
//    }
//
////    public static boolean couple(String id, List<FdgEdgeModel> ListOfLink) {
////        boolean flag = false;
////        for (FdgEdgeModel edgeModel : ListOfLink) {
////            if (edgeModel.getSource().equals(id) || edgeModel.getTarget().equals(id)) {
////                return true;
////            }
////        }
////        return flag;
////    }
////	private static void clearData(String projectPath, Connection conn) {
////		SimpleASTHelper.removeRoot(projectPath);
////		// DBConnection.releaseConn(conn);
////		System.gc();
////	}

    public static ProjectInfo calculateInfo(JProjectNode root) {
        ProjectInfo projectInfo = new ProjectInfo(root);
        projectInfo.calculateProjectInfo();
        return projectInfo;
    }

//	private static void storeInfo(Connection conn, ProjectInfo projectInfo, long projID, long verID) {
//		// System.out.println("waiting...");
//		try {
//			MethodDao.clearTable(conn, projID, verID);
//			FileDao.clearTable(conn, projID, verID);
//			ComponentDao.clearTable(conn, projID, verID);
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
//		List<ComponentDBModel> listOfComps = new ArrayList<>();
//		List<FileDBModel> listOfFiles = new ArrayList<>();
//		List<FileExtDBModel> listOfFileExts = new ArrayList<>();
//		List<MethodDBModel> listOfMethods = new ArrayList<>();
//		for (ComponentInfo cInfo : projectInfo.getListOfCompInfo()) {
//
//			ComponentDBModel component = new ComponentDBModel(cInfo);
//			listOfComps.add(component);
//			// try {
//			// ComponentDao.insertComponent(conn, component, projID, verID);
//			// } catch (Exception e2) {
//			// e2.printStackTrace();
//			// }
//			for (FileInfo fInfo : cInfo.getListOfFileInfo()) {
//				FileDBModel file = new FileDBModel(fInfo);
//				listOfFiles.add(file);
//				FileExtDBModel fileExt = new FileExtDBModel(fInfo);
//				listOfFileExts.add(fileExt);
//				// try {
//				// FileDao.insertFile(conn, file, projID, verID);
//				// } catch (Exception e1) {
//				// e1.printStackTrace();
//				// }
//				for (MethodInfo mInfo : fInfo.getListOfMethodInfo()) {
//					MethodDBModel method = new MethodDBModel(mInfo);
//					listOfMethods.add(method);
//				}
//			}
//		}
//
////		System.out.println("insert Methods " + listOfMethods.size());
//		MethodDao.insertMethods(conn, listOfMethods, projID, verID);
//		MethodDao.insertFileMethods(conn, listOfMethods, projID, verID);
////		System.out.println("insert Files " + listOfFiles.size());
//		FileDao.insertFiles(conn, listOfFiles, projID, verID);
//		FileExtDao.insertFileExts(conn, listOfFileExts, projID, verID);
////		System.out.println("insert Comps " + listOfComps.size());
//		ComponentDao.insertComponents(conn, listOfComps, projID, verID);
//	}
}

